<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/6/2 14:34 jiahe_new dingSendErrorMsgServer.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\common\common\Server;

class  dingSendErrorMsgServer{


    public static $url = 'https://oapi.dingtalk.com/robot/send?access_token=ac4cecc666eb90e21944a93066f7aca4d294736794a7ddbe2ddc8441b8685ffb';
    public static $error = '';


    /**
     * @param $msg
     * @param string $url
     * @return bool|mixed
     * @author 韩威兵^life <1877361302@qq.com>
     */
    public static function sengMsg($msg, $url = ''){
        if(empty($url)){
            $url = self::$url;
        }
        return self::sendRobotData($msg, $url);
    }


    /**
     * @param $msg
     * @return bool|mixed
     * @author 韩威兵^life <1877361302@qq.com>
     */
    public static function sendRobotData($msg, $url){
        $arr['msgtype'] = 'text';
        $arr['text'] = ['content'=>$msg];

        $res = self::postJson($arr, $url);
        if($res === false){
            return false;
        }else{
            return json_decode($res, true);
        }
    }


    /**
     * @param array $data
     * @param string $url
     * @return bool|mixed
     * @author 韩威兵^life <1877361302@qq.com>
     */
    public static function postJson($data = [], $url = ''){
        $json = json_encode($data);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($json)
        ));

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            self::$error = curl_error($ch);
            return false;
        }
        curl_close($ch);
        return $result;
    }
}  